package com.taotao.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.taotao.mapper.*;
import com.taotao.pojo.*;
import com.taotao.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taotao.common.enums.GoodsStatus;
import com.taotao.common.enums.OperateType;
import com.taotao.common.pojo.BaseResult;
import com.taotao.common.utils.Constants;
import com.taotao.common.utils.DateUtil;
import com.taotao.common.utils.RedisUtils;
import com.taotao.mapperCust.EyGoodsMapperCust;

@Service
public class GoodsServiceImpl implements GoodsService {

	@Autowired
	EyGoodsMapper eyGoodsMapper;
	@Autowired
	EyReportMapper eyReportMapper;
	@Autowired
	EyViewHistoryMapper eyViewHistoryMapper;
	@Autowired
	EyGoodsMapperCust eyGoodsMapperCust;
	@Autowired
	EyCollectMapper eyCollectMapper;
	@Autowired
	EyCategoryMapper eyCategoryMapper;
	@Autowired
	EySessionInfoMapper eySessionInfoMapper;
	@Autowired
	EyMessageInfoMapper eyMessageInfoMapper;
	@Autowired
	EyMemberMapper eyMemberMapper;
	@Autowired
	EyPictureMapper eyPictureMapper;
	@Autowired
	PictureService pictureService;
	@Autowired
	WechatService wechatService;
	@Autowired
	OperateLogService operateLogService;
	@Autowired
	EyOperateLogMapper eyOperateLogMapper;
	@Autowired
	CategoryService categoryService;
	@Autowired
	EyOfficialAccountsMapper eyOfficialAccountsMapper;
	@Autowired
	ParamService paramService;
	@Autowired
	CommonService commonService;
	@Autowired
	ChargeService chargeService;
	
	BaseResult baseResult = new BaseResult();
	
	@Override
	public Map<String, Object> goodsAdd(Map<String, String> map) {
		try {
			String memberId=map.get("memberId");
			EyMember member = eyMemberMapper.selectByPrimaryKey(Integer.parseInt(memberId));
			Integer publishNum = member.getPublishNum();
			Integer publishNumAble = member.getPublishNumAble();
			
			if(Constants.num_1.equals(member.getIsCharge())){ //开启付费模式
				Map<String,Object> resultMap=chargeService.chargeSubtract(member,Constants.postPayMoney,"Post goods");
				if("-3".equals(resultMap.get("resultCode"))){ //余额不足
					return resultMap;
				}
			}
		
			if(Constants.num_1.equals(member.getIsBlack())){ //黑名单用户不可发布
				return baseResult.getErrorMap("Send failure,please contact  customerservice");
			}
			
			if(publishNum+1> publishNumAble){
				return baseResult.getErrorMap("Exceeds the limit, please contact customer service");
			}
			
			String title = map.get("title");
			title=title.replace("&"," ");
			String details = map.get("details");
			String address = map.get("address");
			String mobile =map.get("mobile");
			String wechat = map.get("wechat");
			String remark = map.get("remark");
			String price = map.get("price");
			String categoryId = map.get("categoryId");
			String cityId = map.get("cityId");
			String cityName= map.get("cityName");
			String districtId = map.get("districtId");
			String districtName = map.get("districtName");
			String centerData = map.get("centerData");


			Map<String, Object> titleBlackCheckResult = paramService.blackContentCheck(title);
			if(BaseResult.ERROR_CODE.equals(titleBlackCheckResult.get("resultCode"))){
				String msg=(String)titleBlackCheckResult.get("resultMsg");
				return baseResult.getErrorMap(msg);
			}

			Map<String, Object> detailsBlackCheckResult = paramService.blackContentCheck(details);
			if(BaseResult.ERROR_CODE.equals(detailsBlackCheckResult.get("resultCode"))){
				String msg=(String)detailsBlackCheckResult.get("resultMsg");
				return baseResult.getErrorMap(msg);
			}

			if(!wechatService.msgCheck(title)){
				return baseResult.getErrorMap("The title field is risky");
			}
			
			if(!wechatService.msgCheck(details)){
				return baseResult.getErrorMap("The details field is risky");
			}

			if(!wechatService.msgCheck(remark)){
				return baseResult.getErrorMap("The remark field is risky");
			}

//			if(!wechatService.msgCheck(address)){
//				return baseResult.getErrorMap("The address field is risky");
//			}
//
//			if(!wechatService.msgCheck(wechat)){
//				return baseResult.getErrorMap("The wechat field is risky");
//			}
			

			
			EyCategory category = eyCategoryMapper.selectByPrimaryKey(Integer.parseInt(categoryId));
			String language=RedisUtils.hGet(Constants.paramMap, "language");
			String categroyName="en".equalsIgnoreCase(language)?category.getCategoryNameEn():category.getCategoryName();
			
			ObjectMapper objectMapper = new ObjectMapper();
			objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
			EyGoods goods = objectMapper.convertValue(map, EyGoods.class);
			goods.setStatus(GoodsStatus.goodsStatus_0.getCode()); //正在发部
			goods.setCollectNum(0);
			goods.setLookupNum(0);
			goods.setMobile(StringUtils.isNotBlank(mobile)?mobile:member.getMobile());
			goods.setWechat(StringUtils.isNotBlank(wechat)?wechat:member.getWechat());
			goods.setPrice(StringUtils.isNotBlank(price)?price:"0");
			goods.setRemark(remark);
			goods.setCategoryName(categroyName);
			goods.setIsPayPlatform(Constants.num_1);  //开启支付
			
			address=StringUtils.isNotBlank(address)?address:cityName+" "+districtName;
			goods.setAddress(address);
			goods.setCity(cityId);
			goods.setDistrict(districtId);
			goods.setCenterData(centerData);
			goods.setCreateTime(DateUtil.getDateAndTime());
			goods.setUpdateTime(DateUtil.getDateAndTime());
			eyGoodsMapper.insert(goods);
			
			member.setPublishNum(member.getPublishNum()+1); //发布数量+1
			member.setUpdateTime(DateUtil.getDateAndTime());
			eyMemberMapper.updateByPrimaryKeySelective(member);
			
			operateLogService.insert(Integer.parseInt(memberId), null,goods.getId(),"发布goods成功",OperateType.operateType_1.getCode(),""); 
			
			Map<String,String> m=new HashMap<>();
			m.put("goodsId", goods.getId().toString());
			return baseResult.getSuccMap(m);
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}


	@Override
	public Map<String, Object> modifyGoods(Map<String, String> map) {
		try {
			String memberId=map.get("memberId");
			String title = map.get("title");
			title=title.replace("&"," ");
			String details = map.get("details");
			String address = map.get("address");
			String mobile =map.get("mobile");
			String wechat = map.get("wechat");
			String remark = map.get("remark");
			String price = map.get("price");
			String categoryId = map.get("categoryId");
			String goodsId = map.get("goodsId");
			String cityId = map.get("cityId");
			String cityName= map.get("cityName");
			String districtId = map.get("districtId");
			String districtName = map.get("districtName");
			String centerData = map.get("centerData");
			
			EyMember member = eyMemberMapper.selectByPrimaryKey(Integer.parseInt(memberId));
			
			if(Constants.num_1.equals(member.getIsCharge())){ //开启付费模式
				Map<String,Object> resultMap=chargeService.chargeSubtract(member,Constants.modifyPayMoney,"Modify goods");
				if("-3".equals(resultMap.get("resultCode"))){ //余额不足
					return resultMap;
				}
			}


			Map<String, Object> titleBlackCheckResult = paramService.blackContentCheck(title);
			if(BaseResult.ERROR_CODE.equals(titleBlackCheckResult.get("resultCode"))){
				String msg=(String)titleBlackCheckResult.get("resultMsg");
				return baseResult.getErrorMap(msg);
			}

			Map<String, Object> detailBlackCheckResult = paramService.blackContentCheck(details);
			if(BaseResult.ERROR_CODE.equals(detailBlackCheckResult.get("resultCode"))){
				String msg=(String)detailBlackCheckResult.get("resultMsg");
				return baseResult.getErrorMap(msg);
			}

			if(!wechatService.msgCheck(title)){
				return baseResult.getErrorMap("The title field is risky");
			}

			if(!wechatService.msgCheck(details)){
				return baseResult.getErrorMap("The details field is risky");
			}

			if(!wechatService.msgCheck(remark)){
				return baseResult.getErrorMap("The remark field is risky");
			}
//			if(!wechatService.msgCheck(address)){
//				return baseResult.getErrorMap("The address field is risky");
//			}
//
//			if(!wechatService.msgCheck(wechat)){
//				return baseResult.getErrorMap("The wechat field is risky");
//			}

			
			EyCategory category = eyCategoryMapper.selectByPrimaryKey(Integer.parseInt(categoryId));
			String language=RedisUtils.hGet(Constants.paramMap, "language");
			String categroyName="en".equalsIgnoreCase(language)?category.getCategoryNameEn():category.getCategoryName();
			
			EyGoods goods = new EyGoods();
			goods.setId(Integer.parseInt(goodsId));
			goods.setTitle(title);
			goods.setDetails(details);
			goods.setMobile(StringUtils.isNotBlank(mobile)?mobile:member.getMobile());
			goods.setWechat(StringUtils.isNotBlank(wechat)?wechat:member.getWechat());
			goods.setPrice(StringUtils.isNotBlank(price)?price:"0");
			goods.setCategoryId(Integer.parseInt(categoryId));
			goods.setCategoryName(categroyName);
			goods.setRemark(remark);
			address=StringUtils.isNotBlank(address)?address:cityName+" "+districtName;
			goods.setAddress(address);
			goods.setCity(cityId);
			goods.setDistrict(districtId);
			goods.setCenterData(centerData);
			goods.setUpdateTime(DateUtil.getDateAndTime());
			eyGoodsMapper.updateByPrimaryKeySelective(goods);
			
			operateLogService.insert(Integer.parseInt(memberId), null,goods.getId(),"修改goods成功",OperateType.operateType_2.getCode(),""); 
			
			Map<String,String> m=new HashMap<>();
			m.put("goodsId", goods.getId().toString());
			return baseResult.getSuccMap(m);
			
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}
	
	@Override
	public Map<String, Object> getGoodsList(Map<String, String> map) {
		try {
			String pageNum =  map.get("pageNum");
			String pageSize = map.get("pageSize");
			String categoryId=map.get("categoryId");
			String city=map.get("city");
			String district=map.get("district");
			String priceId=map.get("priceId");
			if(Constants.num_1.equals(city)){ //city为1查china
				map.put("city", "");
			}
			if(Constants.num_1.equals(district)){ 
				map.put("district", "");
			}
			Integer num=StringUtils.isBlank(pageNum)?Constants.pageNum:Integer.parseInt(pageNum);
			Integer size=StringUtils.isBlank(pageSize)?Constants.pageSize:Integer.parseInt(pageSize);
	  		if(StringUtils.isNotBlank(categoryId)){
	  			EyCategory category = eyCategoryMapper.selectByPrimaryKey(Integer.parseInt(categoryId));
	  			if(category!=null){
	  				map.put("categoryId", categoryId);
	  				if("0".equals(String.valueOf(category.getParentId()))){//父分类
	  					map.put("parentId", categoryId);
	  					map.put("categoryId", ""); 
	  				}
	  			}else{
	  				map.put("categoryId", ""); //为空的时候查询所有
	  			}
	  		}
	  		
	  		this.handlePrice(map,priceId);
	  		
	  		map.put("status", GoodsStatus.goodsStatus_1.getCode());  //查询有效的数据
	  		PageHelper.startPage(num,size);
			List<Map> goodsList = eyGoodsMapperCust.getGoodsList(map);
			String tencentCosEnable=paramService.getParam(Constants.tencent_cos_enable);
			for(Map m:goodsList){
				String goodsId=String.valueOf(m.get("id"));
				String memberId=String.valueOf(m.get("memberId"));
				
				EyPictureExample exampleGoodsPic = new EyPictureExample();
				com.taotao.pojo.EyPictureExample.Criteria criteriaPic = exampleGoodsPic.createCriteria();
				criteriaPic.andGoodsIdEqualTo(Integer.parseInt(goodsId));
				criteriaPic.andStatusNotEqualTo(Constants.num_100);
		  		exampleGoodsPic.setOrderByClause(" image_index asc ");
				List<EyPicture> listPic = eyPictureMapper.selectByExample(exampleGoodsPic);
				//转换文件地址
				if(Constants.false_string.equals(tencentCosEnable)) {
					pictureService.changeDomain(listPic);
				}

				m.put("listPic", listPic);
				
				EyMember member = eyMemberMapper.selectByPrimaryKey(Integer.parseInt(memberId));
				//如果设置了自己的头像，则使用自己的头像，
				if(StringUtils.isNotBlank(member.getImageUrl())){
					member.setAvatarUrl(member.getImageUrl());
				}
				m.put("member", member);

//				String updateTime=String.valueOf(m.get("updateTime"));
//				String createTime=String.valueOf(m.get("createTime"));
//				updateTime=DateUtil.fromToday(DateUtil.strToDate(updateTime), paramService.getParam("language"));
//				m.put("updateTime", updateTime);
//				createTime=DateUtil.fromToday(DateUtil.strToDate(createTime), paramService.getParam("language"));
//				m.put("createTime", createTime);
				
			}
			
			PageInfo<Map> pageInfo = new PageInfo<>(goodsList);
	  		return baseResult.getSuccMap(pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}
	
	@Override
	public Map<String, Object> queryGoodsList(Map<String, String> map) {
		try {
			String pageNum =  map.get("pageNum");
			String pageSize = map.get("pageSize");
			Integer num=StringUtils.isBlank(pageNum)?Constants.pageNum:Integer.parseInt(pageNum);
			Integer size=StringUtils.isBlank(pageSize)?Constants.pageSize:Integer.parseInt(pageSize);
	  		
	  		PageHelper.startPage(num,size);
			List<Map> goodsList = eyGoodsMapperCust.getGoodsList(map);
			for(Map m:goodsList){
				String goodsId=String.valueOf(m.get("id"));
				String memberId=String.valueOf(m.get("memberId"));
				String updatedAt=String.valueOf(m.get("updatedAt"));
				String createTime=String.valueOf(m.get("createTime"));
				String status=String.valueOf(m.get("status"));
				String isPayPlatform=String.valueOf(m.get("isPayPlatform"));
				m.put("statusDesc", GoodsStatus.getValue(status));
				m.put("isPayPlatformDesc", Constants.num_1.equals(isPayPlatform)?Constants.yes:Constants.no);
				m.put("createTime", DateUtil.getDateTimeFormatStr(createTime));
				
				if(StringUtils.isNotBlank(updatedAt)){
					m.put("updatedAt", DateUtil.getDateTimeFormatStr(updatedAt));
				}
				
				EyMember member = eyMemberMapper.selectByPrimaryKey(Integer.parseInt(memberId));
				//如果设置了自己的头像，则使用自己的头像，
				if(StringUtils.isNotBlank(member.getImageUrl())){
					member.setAvatarUrl(member.getImageUrl());
				}
				m.put("nickName", member.getNickName());
				
				Map<String, String> memberSubscribe = commonService.querySubscribe(member.getUnionId());
				m.put("memberSubscribe",memberSubscribe);
				
			}
			
			PageInfo<Map> pageInfo = new PageInfo<>(goodsList);
	  		return baseResult.getSuccMap(pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}

	
	private void handlePrice(Map<String, String> map, String priceId) {
		if(Constants.num_1.equals(priceId)){
			map.put("startPrice", "0");
			map.put("endPrice", "4000");
		}else if(Constants.num_2.equals(priceId)){
			map.put("startPrice", "4000");
			map.put("endPrice", "8000");
		}else if(Constants.num_3.equals(priceId)){
			map.put("startPrice", "8000");
			map.put("endPrice", "12000");
		}else if(Constants.num_4.equals(priceId)){
			map.put("startPrice", "12000");
			map.put("endPrice", "16000");
		}else if(Constants.num_5.equals(priceId)){
			map.put("startPrice", "16000");
			map.put("endPrice", "20000");
		}else if(Constants.num_6.equals(priceId)){
			map.put("startPrice", "20000");
			map.put("endPrice", "200000");
		}
		
	}


	@Override
	public Map<String, Object> getCollectList(Map<String, String> map) {
		try {
			String memberId=map.get("memberId");
			String type=String.valueOf(map.get("type")); //商品查询类型 0 分类的查询，1 表示我的发布 2 表示我的收藏 3我的足迹
			EyMember member = eyMemberMapper.selectByPrimaryKey(Integer.parseInt(memberId));
			if(member==null){
				return baseResult.getErrorMap("会员编号不存在,memberId:"+memberId);
			}
			
			String pageNum =  map.get("pageNum");
			String pageSize = map.get("pageSize");
			Integer num=StringUtils.isBlank(pageNum)?Constants.pageNum:Integer.parseInt(pageNum);
			Integer size=StringUtils.isBlank(pageSize)?Constants.pageSize:Integer.parseInt(pageSize);
			
			List<Map> goodsList =new ArrayList<Map>();
			PageHelper.startPage(num,size);
			if(Constants.num_1.equals(type)){ //我的发布
				if(Constants.num_1.equals(member.getIsAdmin())){ //管理员查询全部
					map.put("memberId","");
				}
				goodsList=eyGoodsMapperCust.getGoodsList(map);
			}else if("2".equals(type)){ //我的收藏
				goodsList = eyGoodsMapperCust.getCollectList(map);
			}
			String tencentCosEnable=paramService.getParam(Constants.tencent_cos_enable);
			for(Map m:goodsList){
				String createTime=String.valueOf(m.get("createTime"));
				String updateTime=String.valueOf(m.get("updateTime"));
				String imageUrl=String.valueOf(m.get("imageUrl"));
				m.put("createTime",DateUtil.getDateTimeFormatStr(createTime));
				m.put("updateTime",DateUtil.getDateTimeFormatStr(updateTime));
				if(Constants.false_string.equals(tencentCosEnable)) {
					m.put("imageUrl",pictureService.changeDomain(imageUrl));
				}
			}
			PageInfo<Map> pageInfo = new PageInfo<>(goodsList);
	  		return baseResult.getSuccMap(pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}

	@Override
	public Map<String, Object> getGoodsInfo(Map<String, String> map) {
		try {
			String goodsId=map.get("goodsId");
			String memberId=map.get("memberId");
			EyGoods goods = eyGoodsMapper.selectByPrimaryKey(Integer.parseInt(goodsId));
			
			Integer categoryId = goods.getCategoryId();
			EyCategory category = eyCategoryMapper.selectByPrimaryKey(categoryId);
			String isPayPlatform_goods = paramService.getParam(Constants.isPayPlatform_goods);
			if(Constants.num_3.equals(category.getParentId().toString())){  //房产类的支付按钮不可用
				goods.setIsPayPlatform("0");  //不可用
			}else{
				goods.setIsPayPlatform(isPayPlatform_goods);  //goods类，param表中取出，1可以用，0不可用
			}
			
			Map<String,Object> returnMap=new HashMap<String,Object>();
			EyPictureExample exampleGoodsPic = new EyPictureExample();
			com.taotao.pojo.EyPictureExample.Criteria criteriaPic = exampleGoodsPic.createCriteria();
			criteriaPic.andGoodsIdEqualTo(Integer.parseInt(goodsId));
			criteriaPic.andStatusNotEqualTo(Constants.num_100);
	  		exampleGoodsPic.setOrderByClause(" id asc ");
			List<EyPicture> listPicture = eyPictureMapper.selectByExample(exampleGoodsPic);

			String tencentCosEnable=paramService.getParam(Constants.tencent_cos_enable);
			List<String> picList=new ArrayList<>();
			for(EyPicture p:listPicture){
				//转换文件地址
				String imageUrl=p.getImageUrl();
				if(Constants.false_string.equals(tencentCosEnable)) {
					imageUrl = pictureService.changeDomain(imageUrl);
				}
				picList.add(imageUrl);
			}
			
			this.addViewHistory(goods,memberId);
			
			String address=goods.getAddress();
			String mobile=goods.getMobile();
			String wechat=goods.getWechat();
			goods.setMobile(StringUtils.isNotBlank(mobile)?mobile:"Null");
			goods.setWechat(StringUtils.isNotBlank(wechat)?wechat:"Unknown");
			goods.setAddress(StringUtils.isNotBlank(address)?address:"Unknown");
			
			if(StringUtils.isBlank(memberId)){//未登录，微信号手机号不可见
				goods.setWechat("");
				goods.setMobile("");
			}else{
				EyMember m = eyMemberMapper.selectByPrimaryKey(Integer.parseInt(memberId));
				String isBlack= m.getIsBlack();
				if("1".equals(isBlack)){ //黑名单用户 微信号手机号不可见
					goods.setWechat("");
					goods.setMobile("");
				}
			}

			String updateTime = goods.getUpdateTime();
			goods.setUpdateTime(DateUtil.getDateTimeFormatStr4(updateTime));

			goods.setImageUrl(goods.getImageUrl());
			EyMember member = eyMemberMapper.selectByPrimaryKey(goods.getMemberId());
			//如果设置了自己的头像，则使用自己的头像，
			if(StringUtils.isNotBlank(member.getImageUrl())){
				member.setAvatarUrl(member.getImageUrl());
			}
			returnMap.put("member", member);
			returnMap.put("goods", goods);
			returnMap.put("picList", picList);
			return baseResult.getSuccMap(returnMap);
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}
	

	/**
	 * 添加浏览记录
	 * @param goods
	 * @param memberId
	 */
	private void addViewHistory(EyGoods goods, String memberId) {
		Integer mId=null;
		//浏览次数加1
		if(goods.getLookupNum()==null){
			goods.setLookupNum(0);
		}
		Random r=new Random();
		int nextInt = r.nextInt(5); //取随机数
		goods.setLookupNum(goods.getLookupNum()+nextInt);
		eyGoodsMapper.updateByPrimaryKey(goods);
		if(StringUtils.isNotBlank(memberId)){//添加浏览记录
			EyViewHistoryExample example = new EyViewHistoryExample();
			com.taotao.pojo.EyViewHistoryExample.Criteria  criteria = example.createCriteria();
	  		criteria.andMemberIdEqualTo(Integer.parseInt(memberId));
	  		criteria.andGoodsIdEqualTo(goods.getId());
	  		
	  		List<EyViewHistory> list = eyViewHistoryMapper.selectByExample(example);
	  		if(list!=null && list.size()>0){
	  			EyViewHistory eyViewHistory = list.get(0);
	  			eyViewHistory.setUpdateTime(DateUtil.getDateAndTime());
	  			eyViewHistoryMapper.updateByPrimaryKeySelective(eyViewHistory);
	  		}else{
	  			EyViewHistory h=new EyViewHistory();
				h.setGoodsId(goods.getId());
				h.setMemberId(Integer.parseInt(memberId));
				h.setCreateTime(DateUtil.getDateAndTime());
				h.setUpdateTime(DateUtil.getDateAndTime());
				eyViewHistoryMapper.insert(h);
	  		}
	  		
	  		mId=Integer.parseInt(memberId);
		}
		operateLogService.insert(mId, null,goods.getId(),OperateType.operateType_25.getDesc(),OperateType.operateType_25.getCode(),""); 
	}

	
	@Override
	public Map<String, Object> modifyGoodsStatus(Map<String, String> map) {
		try {
			String goodsId=map.get("goodsId");
			String status=map.get("status");
			String type=map.get("type");
			String value = GoodsStatus.getValue(status);
			if(StringUtils.isBlank(value)){
				return baseResult.getErrorMap("修改的状态不合法");
			}
			EyGoods goods = eyGoodsMapper.selectByPrimaryKey(Integer.parseInt(goodsId));
			if(goods==null){
				return baseResult.getErrorMap("商品编号不存在,id:"+goodsId);
			}
			EyGoods g=new EyGoods();
			g.setId(Integer.parseInt(goodsId));
			g.setStatus(status);
			g.setUpdateTime(DateUtil.getDateAndTime());
			eyGoodsMapper.updateByPrimaryKeySelective(g);
			
			//status 0正在发布 1在售 2已售   type 1发布时修改  2用户改状态
			String operateType="";
			if("1".equals(type)){
				operateType=OperateType.operateType_18.getCode();
			}else{
				if("1".equals(status)){
					operateType=OperateType.operateType_4.getCode();
				}else if("2".equals(status)){
					operateType=OperateType.operateType_5.getCode();
				}
			}
			
			operateLogService.insert(goods.getMemberId(), null,Integer.parseInt(goodsId),"修改goods状态，状态改为："+status,operateType,""); 
			
			return baseResult.getSuccMap();
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}


	@Override
	public Map<String, Object> deleteGoods(Map<String, String> map) {
		try {
			String goodsId=map.get("goodsId");
			EyGoods goods = eyGoodsMapper.selectByPrimaryKey(Integer.parseInt(goodsId));
			if(goods==null){
				return baseResult.getErrorMap("goodsId不存在,id:"+goodsId);
			}
			
			Integer memberId = goods.getMemberId();
			//发布数量-1
			EyMember member = eyMemberMapper.selectByPrimaryKey(memberId);
			member.setPublishNum(member.getPublishNum()-1);
			member.setUpdateTime(DateUtil.getDateAndTime());
			eyMemberMapper.updateByPrimaryKey(member);
			
			goods.setStatus(GoodsStatus.goodsStatus_100.getCode());
			eyGoodsMapper.updateByPrimaryKey(goods);
			
			operateLogService.insert(goods.getMemberId(), null,Integer.parseInt(goodsId),"删除goods成功",OperateType.operateType_7.getCode(),""); 
		
			return baseResult.getSuccMap();
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}

	
	@Override
	public Map<String, Object> reportGoods(Map<String, String> map) {
		try {
			String memberId=map.get("memberId");
			ObjectMapper objectMapper = new ObjectMapper();
			objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
			EyReport eyReport = objectMapper.convertValue(map, EyReport.class);
			eyReport.setCreateTime(DateUtil.getDateAndTime());
			eyReportMapper.insert(eyReport);
			
			EyMember member = eyMemberMapper.selectByPrimaryKey(Integer.parseInt(memberId));
			member.setPublishNum(member.getPublishNum()+1); //发布数量+1
			member.setUpdateTime(DateUtil.getDateAndTime());
			eyMemberMapper.updateByPrimaryKeySelective(member);
			
			return baseResult.getSuccMap();
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}

	@Override
	public Map<String, Object> setTopCheck(Map<String, String> map) {
		try {
			String memberId=map.get("memberId");
			String setTopNum = paramService.getParam(Constants.setTopNum);
			EyOperateLogExample example = new EyOperateLogExample();
			com.taotao.pojo.EyOperateLogExample.Criteria criteria = example.createCriteria();
			criteria.andMemberIdEqualTo(Integer.parseInt(memberId));
			criteria.andCreateTimeGreaterThan(DateUtil.getDate());
			criteria.andOperateTypeEqualTo(OperateType.operateType_6.getCode());
			List<EyOperateLog> eyOperateLogs = eyOperateLogMapper.selectByExample(example);
			if(eyOperateLogs!=null && eyOperateLogs.size()>0){
				Integer topNum=eyOperateLogs.size();
				if(topNum>=Integer.parseInt(setTopNum)){
					return baseResult.getErrorMap("Limit, you can set top "+setTopNum+" times one day");
				}
			}
			return baseResult.getSuccMap();
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}


	@Override
	public Map<String, Object> setTop(Map<String, String> map) {
		try {
			String memberId=map.get("memberId");
			String goodsId=map.get("goodsId");
			EyMember member = eyMemberMapper.selectByPrimaryKey(Integer.parseInt(memberId));
			if(Constants.num_1.equals(member.getIsCharge())){ //开启付费模式
				Map<String,Object> resultMap=chargeService.chargeSubtract(member,Constants.setTopPayMoney,"Set top");
				if("-3".equals(resultMap.get("resultCode"))){ //余额不足
					return resultMap;
				}
			}

			Map<String, Object> resultMap = this.setTopCheck(map);
			if(BaseResult.SUCCESS_CODE.equals(resultMap.get("resultCode"))){
				EyGoods goods = eyGoodsMapper.selectByPrimaryKey(Integer.parseInt(goodsId));
				goods.setUpdatedAt(DateUtil.getDateAndTime());
				goods.setUpdateTime(DateUtil.getDateAndTime());
				eyGoodsMapper.updateByPrimaryKey(goods);
				operateLogService.insert(Integer.parseInt(memberId), null,Integer.parseInt(goodsId),"置顶成功",OperateType.operateType_6.getCode(),"");
			}
			return baseResult.getSuccMap();
		} catch (Exception e) {
			e.printStackTrace();
			return baseResult.getErrorMap("网络繁忙，请稍后再试");
		}
	}
	
}
